﻿using UnityEngine;
/// <summary>
/// 算法扩展
/// </summary>
public static class SFAlgorithmExtend
{
    /// <summary>
    /// 获得下一个步长值
    /// </summary>
    /// <param name="_curVal">当前值</param>
    /// <param name="_max">最大值</param>
    /// <param name="_step">步长</param>
    /// <param name="_startVal">起始值</param>
    /// <returns></returns>
    public static int NextStepValue(this int _curVal, int _max = int.MaxValue, int _step = 1, int _startVal = 0)
    {
        _curVal += _step;
        if (_curVal > _max)
        {
            _curVal = _startVal;
        }
        return Mathf.Clamp(_curVal, _startVal, _max);
    }

    /// <summary>
    /// 是否是2的幂
    /// </summary>
    /// <param name="_num">数</param>
    /// <returns>true:是2的幂，false:非2的幂</returns>
    public static bool IsPowerOfTwo(this int _num)
    {
        return (_num > 0) && (_num & (_num - 1)) == 0;
    }

    /// <summary>
    /// 将两个整形值交换
    /// </summary>
    /// <param name="_self"></param>
    /// <param name="_swap"></param>
    /// https://www.cnblogs.com/cdaniu/p/16438885.html
    /// 异或也叫半加运算，其运算法则相当于不带进位的二进制加法：二进制下用1表示真，0表示假，则异或的运算法则为：0⊕0=0，1⊕0=1，0⊕1=1，1⊕1=0（同为0，异为1），这些法则与加法是相同的，只是不带进位，所以异或常被认作不进位
    /// <returns></returns>    
    public static int Swap(this int _self, ref int _swap)
    {
        _self = _self ^ _swap;
        _swap = _self ^ _swap;
        //_swap 变成了原来的_self; 
        _self = _self ^ _swap;
        return _self;
    }
}
